package com.swpu.service_xueya.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_xueya.entity.XuetangRecord;
import com.swpu.service_xueya.entity.XueyaRecord;
import com.swpu.service_xueya.entity.vo.*;
import com.swpu.service_xueya.mapper.XuetangRecordMapper;
import com.swpu.service_xueya.service.XuetangRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-04-21
 */
@Service
public class XuetangRecordServiceImpl extends ServiceImpl<XuetangRecordMapper, XuetangRecord> implements XuetangRecordService {

    @Override
    public String addXuetangData(String openid, XuetangRecord xuetangRecord) {
        xuetangRecord.setUserId(openid);
        boolean save = this.save(xuetangRecord);
        if (!save) throw new RuntimeException("保存血压失败");
        return xuetangRecord.getId();
    }

    @Override
    public XuetangRecord getXuetangDataById(String recordId) {
        XuetangRecord xuetangRecord = this.getById(recordId);
        xuetangRecord.setStatus(getXuetangStatus(xuetangRecord.getMeasureValue(),xuetangRecord.getMeasureDurationId()));
        return xuetangRecord;
    }

    @Override
    public Map getRecentMonthXuetangStatus(String openid) {
        Date date = new Date();
        DateTime endDate = DateUtil.endOfDay(date);
        DateTime startDate = DateUtil.offsetDay(endDate, -30);
        LambdaQueryWrapper<XuetangRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XuetangRecord::getUserId,openid);
        queryWrapper.between(XuetangRecord::getRecordTime,startDate,endDate);
        List<XuetangRecord> recordList = this.list(queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",recordList.size());
        int count=0;
        for (XuetangRecord record : recordList) {
            if(getXuetangStatus(record.getMeasureValue(),record.getMeasureDurationId())==1){
                count++;
            }
        }
        map.put("normal",count);
        return map;
    }

    @Override
    public List getNewestDayXuetangDataList(String openid) {
        LambdaQueryWrapper<XuetangRecord> queryWrapper_ = new LambdaQueryWrapper<>();
        queryWrapper_.eq(XuetangRecord::getUserId,openid);
        queryWrapper_.orderByDesc(XuetangRecord::getRecordTime);
        XuetangRecord first = this.getOne(queryWrapper_);
        LambdaQueryWrapper<XuetangRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XuetangRecord::getUserId,openid);
        Date date = first.getRecordTime();
        DateTime endOfDay = DateUtil.endOfDay(date);
        DateTime beginOfDay = DateUtil.beginOfDay(date);
        queryWrapper.between(XuetangRecord::getRecordTime,beginOfDay,endOfDay);
        List<XuetangRecord> recordList = this.list(queryWrapper);
        return recordList;
    }

    @Override
    public XuetangChartData getChartData(String openid,Integer measureDurationId) {
        List<XuetangRecordVo> recordVoList = baseMapper.getAvgXuetangForWeek(openid,measureDurationId);
        XuetangChartData xuetangChartData = new XuetangChartData();
        ArrayList<Integer> avgList = new ArrayList<>();
        ArrayList<Date> recordTimeList = new ArrayList<>();
        //结束时间
        Date endTime = null;
        if (recordVoList.size() == 0) {
            endTime = DateUtil.endOfDay(new Date());
            for (int i=0;i<7;i++){
                avgList.add(null);
               // lowList.add(null);
                recordTimeList.add(DateUtil.endOfDay(endTime));
                endTime=DateUtil.offsetDay(endTime,-1);
            }
        } else if(recordVoList.size() <7) {
            for(int i=0;i<recordVoList.size();i++){
                avgList.add(recordVoList.get(i).getAvgValue());
              //  lowList.add(recordVoList.get(i).getAvgLow());
                recordTimeList.add(DateUtil.endOfDay(recordVoList.get(i).getRecordTime()));
            }
            //结束时间点------填充
            endTime = DateUtil.endOfDay(recordVoList.get(recordVoList.size()-1).getRecordTime());
            for (int i = recordVoList.size(); i <7; i++) {
                //数据库中不存在这天的数据
                avgList.add(null);
          //      lowList.add(null);
                endTime=DateUtil.offsetDay(endTime,-1);
                recordTimeList.add(endTime);
            }
        }else {
            for (XuetangRecordVo xuetangRecordVo : recordVoList) {
                avgList.add(xuetangRecordVo.getAvgValue());
                recordTimeList.add(xuetangRecordVo.getRecordTime());
            }
        }
        xuetangChartData.setRecordTime(recordTimeList);
        xuetangChartData.setAvgValue(avgList);
        return xuetangChartData;
    }

    @Override
    public List<XuetangRecordListVo> getXuetangDataList(String openid, Integer pageNum, Integer pageSize) {
        int start=(pageNum-1)*pageSize;
        List<XuetangRecordVo2> recordList =   baseMapper.getXuetangDataList(openid,start,pageSize);
        ArrayList<XuetangRecordListVo> resultList = new ArrayList<>();
        Date recordDate=null;
        int i=0;
        for (i=0;i<recordList.size();) {
            XuetangRecordVo2 record = recordList.get(i);
            if(recordDate==null||record.getRecordDate().getTime()!=recordDate.getTime()){
                recordDate=record.getRecordDate();
                XuetangRecordListVo xuetangRecordListVo = new XuetangRecordListVo();
                xuetangRecordListVo.setRecordDate(record.getRecordDate());
                ArrayList<XuetangRecordVo2> nowDateXueyaRecordList = new ArrayList<>();
                for(int j=i;j<recordList.size();){
                    if(recordList.get(j).getRecordDate().getTime()==recordDate.getTime()){
                        XuetangRecordVo2 xuetangRecordVo2 = new XuetangRecordVo2();
                        xuetangRecordVo2.setStatus(getXuetangStatus(recordList.get(j).getMeasureValue(),recordList.get(j).getMeasureDurationId()));
                        xuetangRecordVo2.setRecordTime(recordList.get(j).getRecordTime());
                        xuetangRecordVo2.setMeasureValue(recordList.get(j).getMeasureValue());
                        xuetangRecordVo2.setMeasureDurationId(recordList.get(j).getMeasureDurationId());
                        xuetangRecordVo2.setMeasureDurationName(recordList.get(j).getMeasureDurationName());
                        nowDateXueyaRecordList.add(xuetangRecordVo2);
                    }else {
                        i=j;
                        break;
                    }
                    j++;
                    if(j==recordList.size()){
                        i=j;
                    }
                }
                xuetangRecordListVo.setRecordList(nowDateXueyaRecordList);
                resultList.add(xuetangRecordListVo);
            }
        }
        return  resultList;
    }

    @Override
    public XuetangRecord getXuetangDataNewest(String openid) {
        LambdaQueryWrapper<XuetangRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XuetangRecord::getUserId,openid).orderByDesc(XuetangRecord::getRecordTime);
        Date date = new Date();
        DateTime beginOfDay = DateUtil.beginOfDay(date);
        DateTime endOfDay = DateUtil.endOfDay(date);
        queryWrapper.between(XuetangRecord::getRecordTime,beginOfDay,endOfDay);
        XuetangRecord record = this.getOne(queryWrapper);
        Integer status = getXuetangStatus(record.getMeasureValue(), record.getMeasureDurationId());
        record.setStatus(status);
        return record;
    }


    public Integer getXuetangStatus(Double value,Integer durationId) {
        if (durationId == 0) {
            if (value <= 6.9 && value > 4.4) {
                return 1;
            } else if (value <= 4.4 && value > 3.9) {
                return 0;
            } else if (value <= 3.9) {
                return -1;
            } else if (value > 6.9 && value <= 16.6) {
                return 2;
            } else {
                return 3;
            }
        } else if (durationId == 1 || durationId == 2 || durationId == 3 || durationId == 4 || durationId == 5 || durationId == 6 || durationId == 7) {
            if (value <= 3.9) return -1;
            else if (value > 3.9 && value <= 4.4) return 0;
            else if (value > 4.4 && value <= 9.9) return 1;
            else if (value > 9.9 && value <= 16.6) return 2;
            else return 3;
        }
        return 1;
    }
}
